package logger

import (
	"fmt"
	"github.com/sirupsen/logrus"
	"os"
)

var _globalLogger *logrus.Logger

func InitLogger(logName, level string) error {
	_globalLogger = logrus.New()
	SetLogFile(logName)
	SetLevel(level)
	return nil
}

func SetLevel(level string) {
	logLevel, err := logrus.ParseLevel(level)
	if err == nil {
		_globalLogger.SetLevel(logLevel)
	}
}

func SetLogFile(logName string) {
	f, err := os.Create(logName)
	if err != nil {
		fmt.Println("无法写入日志文件， 程序退出")
		os.Exit(1)
	}

	_globalLogger.SetOutput(f)
}

func Debugf(format string, v ...interface{}) {
	_globalLogger.Debugf(format, v...)
}

func Debug(v ...interface{}) {
	_globalLogger.Debug(v...)
}

func Infof(format string, v ...interface{}) {
	_globalLogger.Infof(format, v...)
}

func Info(v ...interface{}) {
	_globalLogger.Info(v...)
}

func Warnf(format string, v ...interface{}) {
	_globalLogger.Warnf(format, v...)
}

func Warn(v ...interface{}) {
	_globalLogger.Warn(v...)
}

func Errorf(format string, v ...interface{}) {
	_globalLogger.Errorf(format, v...)
}

func Error(v ...interface{}) {
	_globalLogger.Error(v...)
}

func Fatalf(format string, v ...interface{}) {
	_globalLogger.Fatalf(format, v...)
}

func Fatal(v ...interface{}) {
	_globalLogger.Fatal(v...)
}
